let app = require("express").Router();
// 解码token 
let { verifyToken } = require('../../token/token');
let Mysql = require('../../mysql/mysql');
let pomfmsq = require('../../mysql/connect/pomsql');

// 获取所有商品信息
app.post('/getPoints', async (req, res) => {
  let sql = `select * from points`;
  let dataList = await Mysql(sql);
  if (!dataList.length) {
    return res.json({ code: 404 })
  }
  res.json({ code: 200, data: dataList })
})

// 获取指定类的商品信息
app.post('/getclass', async (req, res) => {
  let { index } = req.body;
  if (!index && index !== 0) {
    return res.json({ code: 204 });
  }
  let sql;
  if (index === 0) {
    sql = `select * from points`;
  } else {
    sql = `select * from points WHERE state="${index}"`
  }
  let dataList = await Mysql(sql);
  if (!dataList.length) {
    return res.json({ code: 204 });
  }
  res.json({ code: 200, data: dataList })
})

// 添加商品
app.post('/additem', async (req, res) => {
  let { name, title, sort, stock: inventory, suttle: weight, brand, keyword, describes: description, price, points: integration, key, imgList: imageList, state } =
    req.body;
  if (
    !name ||
    !title ||
    !sort ||
    !inventory ||
    !weight ||
    !brand ||
    !keyword ||
    !description ||
    !price ||
    !integration ||
    !imageList ||
    !state
  ) {
    return res.json({ code: 204 });
  }
  let sql;
  if (!key) {
    // 添加
    sql = "INSERT INTO `points` (`name`,`title`,`sort`,`inventory`,`weight`,`brand`,`keyword`,`description`,`price`,`integration`,`imageList`,`state`) VALUES ('" + name + "','" + title + "','" + sort + "','" + inventory + "','" + weight + "','" + brand + "','" + keyword + "','" + description + "','" + price + "','" + integration + "','" + imageList + "','" + state + "')";
  } else {
    // 修改
    sql = "UPDATE points SET name='" + name + "',title='" + title + "',sort='" + sort + "',inventory='" + inventory + "',weight='" + weight + "',brand='" + brand + "',keyword='" + keyword + "',description='" + description + "',price='" + price + "',integration='" + integration + "',imageList='" + imageList + "',state='" + state + "' WHERE `key`='" + key + "'";
  }
  let dataList = await Mysql(sql);
  if (dataList.affectedRows) {
    res.json({ code: 200 })
  } else {
    res.json({ code: 204 })
  }
})

// 删除指定商品
app.post('/deleteItem', async (req, res) => {
  let { key, isdelete, state } = req.body;
  if (!key || !isdelete || !state) {
    res.json({ code: 204 });
  }
  let sql = "UPDATE points SET isdelete='" + isdelete + "',state='" + state + "' WHERE `key`='" + key + "'";
  let dataList = await Mysql(sql);
  let sqlSelect = `select * from points`;
  let dataAll = await Mysql(sqlSelect);
  if (!dataAll.length || !dataList.affectedRows) {
    return res.json({ code: 204 })
  } else {
    res.json({ code: 200, data: dataAll })
  }
})

// 自动登录
app.post('/autoLogin', async (req, res) => {
  let { token } = req.body;
  let data;
  try {
    data = verifyToken(token)
  } catch (error) {
    res.json({ code: 204, msg: '登录过期' })
    return;
  }
  let sql = `SELECT * FROM user_id WHERE user="${data.username}" AND password="${data.password}"`;
  let userData = await pomfmsq(sql);
  if (userData.length > 0) {
    // 携带一个权限
    let visitSql = `SELECT * FROM roles WHERE id="${userData[0].auth}"`
    let visitList = await pomfmsq(visitSql);
    res.json({
      code: 2000,
      msg: "登陆成功",
      token,
      auth: userData[0].auth,
      visit: userData[0].visit,
      visitList
    });
  } else {
    response.json({
      code: 2004,
      msg: "用户名或密码错误",
    });
  }
})

app.post('/getSlide', async (req, res) => {
  let sql = `select * from slideshow`
  let visitList = await pomfmsq(sql);
  if (!visitList.length) {
    res.json({ code: 204 })
    return;
  }
  let newData = [];
  visitList.map(item => {
    if (Number(item.isdelete)) {
      newData.push(item)
    }
  })
  res.json({ code: 200, data: newData })
})

app.post('/deleteSlide', async (req, res) => {
  let { id } = req.body;
  let sql = "UPDATE slideshow SET isdelete='0' WHERE `id`='" + id + "'";
  let visitList = await pomfmsq(sql);
  let newData = [];
  visitList.map(item => {
    if (Number(item.isdelete)) {
      newData.push(item)
    }
  })
  if (!visitList.length) {
    res.json({ code: 204 })
    return;
  }
  res.json({ code: 200, data: newData })
})

module.exports = app;
